/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package teacher;

import common.MessageModel;
import common.Message_Type;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTextArea;

/**
 *
 * @author Eugene-Forest
 */
public class GetHistoryThread extends Thread {

    private JTextArea message_panel = null;
    private String nowdayFileName="";

    public GetHistoryThread(JTextArea message_panel) {
        this.message_panel = message_panel;
    }

    public GetHistoryThread() {
    }

    @Override
    public void run() {
        Date realDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String publishTime = dateFormat.format(realDate);
        String name = "c:/red_spider/history/" + publishTime + ".txt";
        nowdayFileName=publishTime + ".txt";
        File file = new File(name);
        deleteFile(new File("c:/red_spider/history/"));
        if (!file.exists()) {
            //如果文件不存在则终止线程
            return;
        }
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String message;
            while ((message = bufferedReader.readLine()) != null) {
                String[] infoStrings = message.split("<\\|@Cut@\\|>");
                MessageModel messageModel = new MessageModel();
                messageModel.setPublishDate(infoStrings[0]);
                String[] messageStrings = infoStrings[1].split("<\\|@Enter@\\|>");
                String resultString = "";
                for (String msg : messageStrings) {
                    resultString += msg + "\n";
                }
                messageModel.setMessageString(resultString);
                switch (infoStrings[2]) {
                    case "Notice":
                        messageModel.setType(Message_Type.Notice);
                        break;
                    case "Finish":
                        messageModel.setType(Message_Type.Finish);
                        break;
                    case "Review":
                        messageModel.setType(Message_Type.Review);
                        break;
                    default:
                        break;
                }
                if ("true".equals(infoStrings[3])) {
                    messageModel.setReal_time(true);
                } else {
                    messageModel.setReal_time(false);
                }
                DataCenter.add_messageModel(messageModel);
            }
            if (message_panel != null) {
                for (MessageModel mm : DataCenter.get_messageModels()) {
                    message_panel.append(mm.getPublishDate() + "：\n" + mm.getMessageString() + "\n");
                    message_panel.append("----------\n\n");
                }
            }
            //重定位历史位置
            message_panel.setCaretPosition(message_panel.getText().length());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 删除除了本日的历史记录文件
     * @param file 历史记录根目录
     */
     private void deleteFile(File file){
        if(file == null || !file.exists()){
            return;
        }
        //获得文件对象的子文件对象列表
        File[] files = file.listFiles();
        //遍历这些子文件
        for (File f : files){
            //判断这些子文件是否是目录
            if(f.isDirectory()){
                //是文件夹的话继续递归 如果是发现文件直接删除
                deleteFile(f);
            }else if(!f.getName().equals(nowdayFileName)){
                f.delete();
            }
        }
    }

}
